#!/bin/bash

# Slackware build script for vice

# Copyright 2008 Mauro Giachero (email removed)
# Copyright 2020, 2021 B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20210809 bkw: update for v3.5, many changes, can't build old versions.
# - Relicense as WTFPL, with permission from the original author.
# - Build & install the actual HTML docs (not the bundled copy of
#   the upstream website).
# - Don't install fonts. They were part of the above.
# - Use upstream's .desktop files instead of our own.
# - Upstream removed the man pages; restore them (for now).
# - Build the gtk+3 UI. Also build the SDL2 UI, unless the user's
#   requested the SDL1 one (or disabled SDL).
# - Really fix the paths in the documentation.
# - ffmpeg now included in Slackware (pre-15.0 -current). Still
#   autodetected, but the docs no longer mention it.
# - Get rid of README_SBo.txt as the build options are simpler
#   and "fit" in the regular README now.

# 20200801 bkw:
# - Take over maintenance
# - Autodetect ffmpeg
# - Allow building with SDL 1.x
# - Allow building without pulseaudio
# - Get rid of xa dep
# - Install TTF fonts in correct place, get rid of other formats
# - install-info in doinst.sh so vice.info shows up in the info dir
# - Have slack-desc show build options
# - Re-enable the old x64 emulator
# - BUILD=3

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=vice
VERSION=${VERSION:-3.5}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC -pipe"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

# ffmpeg is included in the base OS on post-14.2 -current, but leave
# this here in case someone's doing something clever.
[ -x /usr/bin/ffmpeg ] && ffmpeg="--enable-external-ffmpeg"

# Anticipating 15.0's pure-alsa-system
if [ "${PULSE:-yes}" != "yes" ]; then
  pulseopt="--without-pulse"
fi

echo "=== pulseopt='$pulseopt'"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION

# Fancy tar command here, avoid extracting what we don't need.
tar xvf $CWD/$PRGNAM-$VERSION.tar.?z \
    --wildcards \
    --exclude='*/src/lib/lib*'

cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

DOCDIR=/usr/doc/$PRGNAM-$VERSION
PKGDOC=$PKG/$DOCDIR

# setting TEXMFVAR prevents writing to /root, which SlackBuilds
# really shouldn't do.
export TEXMFVAR=$( pwd )/.texmf-var

# Set proper paths in documentation.
rm -rf doc/vice.pdf # will be rebuilt, along with the HTML doc.
sed -i -e "1,2000s/\/usr\/local/\/usr/g" \
       -e "s,/lib/vice/,/share/vice/,Ig" \
     doc/vice.texi

autoreconf -vif

# We build up to 3 times with slightly different ./configure args,
# so make it a function.
config() {
  local configarg="$1"

  # Avoid the need for xa. It's only used to generate a small C header
  # file, which we include a prebuilt version of here. There's nothing
  # dynamic about this header, it's just a small chunk of 6502 object
  # code as a C array, and for a given version of vice it'll always be
  # the same code. It didn't change between vice 3.4 and 3.5, either.
  cat $CWD/psiddrv.h.prebuilt.3.4 > src/c64/psiddrv.h

  # 20210809 bkw: in 3.5, upstream added dos2unix as a required dep,
  # but it's *only used* if you run "make dist" or "make dist-gzip",
  # which we don't do. DOS2UNIX=/bin/true bypasses the autodetection.
  # XDG_DESKTOP_MENU=/bin/true prevents installing .desktop files,
  # because those don't respect $DESTDIR. We can't just disable them
  # with a configure argument, because that stops them being built
  # at all.

  CFLAGS="$SLKCFLAGS -w -Wl,--allow-multiple-definition" \
  CXXFLAGS="$SLKCFLAGS" \
  DOS2UNIX=/bin/true \
  XA=/bin/true \
  XDG_DESKTOP_MENU=/bin/true \
  ./configure \
    $configarg \
    $ffmpeg \
    $pulseopt \
    --enable-x64 \
    --prefix=/usr \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --mandir=/usr/man \
    --infodir=/usr/info \
    --docdir=$DOCDIR \
    --with-x \
    --enable-quicktime \
    --enable-ethernet \
    --enable-parsid \
    --enable-cpuhistory \
    --with-mpg123 \
    --with-flac \
    --with-vorbis \
    --with-gif \
    --with-jpeg \
    --with-png \
    --enable-midi \
    --enable-lame \
    --build=$ARCH-slackware-linux

  make
}

# SDL UI: *Only* build/install the binaries (not the docs or ROMs),
# rename them to <whatever>-sdl2 (or -sdl1).
buildsdl() {
  local exesuf="-sdl$1"
  local configarg="--disable-pdf-docs --enable-sdlui"
  [ "$1" = "2" ] && configarg+="2"

  config "$configarg"
  make -C src install-binPROGRAMS \
              DESTDIR="$PKG" \
              EXEEXT="$exesuf" \
              INSTALL_PROGRAM="install -s -m0755"

  # get rid of the non-graphical tools (the gtk3 build will
  # install them again without -$exesuf).
  rm -f $PKG/usr/bin/[cp]*$exesuf

  make distclean
}

# Build the SDL binaries, unless disabled.
# Be generous in what we accept for SDL in the env. "both" isn't
# mentioned in README, it's probably only useful to me.
case "${SDL:-2}" in
  ""|2*|y*|Y*|t*|T*) buildsdl 2 ;;
  1*)                buildsdl 1 ;;
  both)              buildsdl 1 ; buildsdl 2 ;;
  n*|N*|f*|F*|0*)    ;; # NOP
  *) cat <<EOF

***
$0: invalid value for SDL environment variable.
Allowed values: "1", "2", "no", "both", or empty/unset (same as "2").
***
EOF
     exit 1 ;;
esac

# Now build & install the whole project with the gtk3 UI.
config "--enable-native-gtk3ui --enable-desktop-files"
make
make install-strip DESTDIR=$PKG

# This is a source file, doesn't belong in the package.
rm -f $PKGDOC/*.texi

# Install desktop files manually.
mkdir -p $PKG/usr/share/desktop-directories \
         $PKG/usr/share/applications
install -m0644 src/arch/gtk3/data/unix/vice*desktop \
        $PKG/usr/share/applications
install -m0644 src/arch/gtk3/data/unix/vice*directory \
        $PKG/usr/share/desktop-directories

# Documentation. vice.pdf has already been installed to the doc dir.
# Generate info, plaintext, and HTML versions here. The text doc is
# harder to navigate, but easier to grep, so it's worth having IMO.
makeinfo doc/$PRGNAM.texi
mkdir -p $PKG/usr/info
mv $PRGNAM.info* $PKG/usr/info

makeinfo --plaintext doc/$PRGNAM.texi > $PKGDOC/$PRGNAM.txt

( cd doc ; texi2any --html $PRGNAM.texi )
cp -a doc/$PRGNAM $PKGDOC/html

# 20210810 bkw: as of 3.5, upstream dropped the man pages. I'm
# including the man pages from 3.4 in the 3.5 package. Edited them
# slightly to add correct paths to the HTML and PDF docs, also
# convert the iso-8859-1 characters into troff escapes. Also
# added (and expanded) vsid.1 from Debian.
mkdir -p $PKG/usr/man/man1
for i in $CWD/man/*.?; do
  sed -e "s,@HTMLDOC@,$DOCDIR/html/index.html," \
      -e "s,@PDFDOC@,$DOCDIR/$PRGNAM.pdf," \
      < $i > $PKG/usr/man/man1/$( basename $i )
done
gzip -9 $PKG/usr/man/man?/*.? $PKG/usr/info/*
for i in x64 x64sc xscpu64 x64dtv x128 xvic xpet xplus4 xcbm2 xcbm5x0; do
  ln -s vice.1.gz $PKG/usr/man/man1/$i.1.gz
done

cp -a NEWS README $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# Make the slack-desc reflect the build options.
PA=no
if [ "$SDL" = "both" ]; then
  SDL="1.2 and 2.0"
else
  SDL=no
  [ -x $PKG/usr/bin/x128-sdl1 ] && SDL=1.2
  [ -x $PKG/usr/bin/x128-sdl2 ] && SDL=2.0
fi
grep -q '^#define USE_PULSE\>' src/config.h && PA=yes

mkdir -p $PKG/install
sed -e "s,@PA@,$PA,"   \
    -e "s,@SDL@,$SDL," \
  $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
